What is lodash.sortby?
The lodash.sortby package is a part of the Lodash library which provides utility functions for common programming tasks using a functional programming paradigm. This package offers a convenient way to sort collections like arrays and lists based on one or more iteratees.
What are lodash.sortby's main functionalities?
Sorting an array of values
This feature allows you to sort an array of objects by a specific property. In the code sample, the 'users' array is sorted by the 'user' property.
const _ = require('lodash.sortby');
const users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 34 }
];
const sortedUsers = _.sortBy(users, [function(o) { return o.user; }]);
Sorting by multiple criteria
This feature allows sorting by multiple criteria. In the code sample, the 'users' array is first sorted by the 'user' property and then by 'age'.
const _ = require('lodash.sortby');
const users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 34 }
];
const sortedUsers = _.sortBy(users, ['user', 'age']);
Other packages similar to lodash.sortby
array-sort
array-sort is a small, fast array sort library that allows you to sort arrays of objects by one or more properties. It is similar to lodash.sortby but does not depend on the entire lodash library.
sort-array
sort-array is another package that provides sorting capabilities for arrays. It can sort by multiple fields and offers a slightly different API compared to lodash.sortby.
lodash.sortby v4.7.0
The lodash method _.sortBy
exported as a Node.js module.
Installation
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.sortby
In Node.js:
var sortBy = require('lodash.sortby');
See the documentation or package source for more details.